import pygame
import random

# 初始化pygame
pygame.init()

# 游戏窗口设置
WIDTH = 800
HEIGHT = 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("坦克大战")

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)

# 坦克类
class Tank(pygame.sprite.Sprite):
    def __init__(self, x, y, color, speed):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((50, 50))
        self.image.fill(color)
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.speed = speed
        self.direction = "up"

    def update(self):
        if self.direction == "up":
            self.rect.y -= self.speed
        elif self.direction == "down":
            self.rect.y += self.speed
        elif self.direction == "left":
            self.rect.x -= self.speed
        elif self.direction == "right":
            self.rect.x += self.speed

        # 边界检测
        if self.rect.left < 0:
            self.rect.left = 0
        if self.rect.right > WIDTH:
            self.rect.right = WIDTH
        if self.rect.top < 0:
            self.rect.top = 0
        if self.rect.bottom > HEIGHT:
            self.rect.bottom = HEIGHT

    def change_direction(self, new_direction):
        self.direction = new_direction

    def shoot(self):
        bullet = Bullet(self.rect.centerx, self.rect.centery, self.direction)
        all_sprites.add(bullet)
        bullets.add(bullet)

# 子弹类
class Bullet(pygame.sprite.Sprite):
    def __init__(self, x, y, direction):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((10, 10))
        self.image.fill(RED)
        self.rect = self.image.get_rect()
        self.rect.centerx = x
        self.rect.centery = y
        self.speed = 8
        self.direction = direction

    def update(self):
        if self.direction == "up":
            self.rect.y -= self.speed
        elif self.direction == "down":
            self.rect.y += self.speed
        elif self.direction == "left":
            self.rect.x -= self.speed
        elif self.direction == "right":
            self.rect.x += self.speed

        # 超出边界销毁子弹
        if self.rect.left < 0 or self.rect.right > WIDTH or self.rect.top < 0 or self.rect.bottom > HEIGHT:
            self.kill()

# 创建精灵组
all_sprites = pygame.sprite.Group()
tanks = pygame.sprite.Group()
bullets = pygame.sprite.Group()

# 创建玩家坦克
player_tank = Tank(WIDTH // 2, HEIGHT - 100, GREEN, 5)
all_sprites.add(player_tank)
tanks.add(player_tank)

# 创建敌方坦克
enemy_tank = Tank(random.randint(0, WIDTH - 50), random.randint(0, 100), RED, 3)
all_sprites.add(enemy_tank)
tanks.add(enemy_tank)

# 游戏主循环
running = True
clock = pygame.time.Clock()

while running:
    clock.tick(60)

    # 处理事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                player_tank.change_direction("up")
            elif event.key == pygame.K_DOWN:
                player_tank.change_direction("down")
            elif event.key == pygame.K_LEFT:
                player_tank.change_direction("left")
            elif event.key == pygame.K_RIGHT:
                player_tank.change_direction("right")
            elif event.key == pygame.K_SPACE:
                player_tank.shoot()

    # 敌方坦克随机移动和射击
    if random.randint(1, 100) < 5:
        directions = ["up", "down", "left", "right"]
        enemy_tank.change_direction(random.choice(directions))
    if random.randint(1, 100) < 2:
        enemy_tank.shoot()

    # 更新所有精灵
    all_sprites.update()

    # 检查子弹与坦克的碰撞
    hits = pygame.sprite.groupcollide(tanks, bullets, True, True)
    for hit in hits:
        if hit == player_tank:
            print("你输了！")
            running = False
        elif hit == enemy_tank:
            print("你赢了！")
            running = False

    # 绘制背景和所有精灵
    screen.fill(BLACK)
    all_sprites.draw(screen)

    # 更新显示
    pygame.display.flip()

pygame.quit()